package com.si.ui;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.LayoutStyle;
import javax.swing.WindowConstants;

import com.si.ui.enumn.EHiResCanvas;

public class HiResCanvas extends JFrame implements IImageCanvas {

	private static final long serialVersionUID = -4628266006820591658L;

	private BufferedImage image;
	
	public final JButton btnRefresh;
	public final JButton btnSave;

	public HiResCanvas(final int width, final int height) {
		this.setResizable(false);
		this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		this.btnRefresh = new JButton();
		this.btnRefresh.setText("Refresh");
		this.btnRefresh.setActionCommand(EHiResCanvas.HiRes_Refresh.toString());
		this.btnSave = new JButton();
		this.btnSave.setText("Save");
		this.btnSave.setActionCommand(EHiResCanvas.HiRes_Save.toString());
		this.setSize(width, height);
	}

	@Override
	public void paint(final Graphics g) {
		super.paint(g);
		if(this.image != null) g.drawImage(this.image, 0, 0, this.image.getWidth(), this.image.getHeight(), null);
	}

	@Override
	public void setImage(final BufferedImage image) {
		this.image = image;
		this.repaint();
	}

	@Override
	public void setSize(final int width, int height) {
		height += 70;
		super.setSize(width, height);
		this.setMinimumSize(new Dimension(width, height));
		this.buildLayout();
	}

	private void buildLayout() {
		GroupLayout layout = new GroupLayout(getContentPane());
		this.getContentPane().setLayout(layout);
		final int gap = this.getWidth()/2 - 100;
		layout.setHorizontalGroup(
				layout.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addGroup(layout.createSequentialGroup()
						.addGap(gap, gap, gap)
						.addComponent(this.btnRefresh)
						.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
						.addComponent(this.btnSave)
						.addContainerGap(108, Short.MAX_VALUE))
		);
		layout.setVerticalGroup(
				layout.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
						.addContainerGap(271, Short.MAX_VALUE)
						.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
								.addComponent(this.btnRefresh)
								.addComponent(this.btnSave))
						.addContainerGap(20, 20))
		);
		this.pack();
	}
}
